package com.david.appletree

import android.content.Intent
import android.graphics.*
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.david.core.utils.DpUtils
import com.david.core.utils.PosterCreator
import java.net.URL
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.sign

class TestActivity:AppCompatActivity() {
    var dates:ArrayList<CalendarUtil.WeekDay>? = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        val bgBitmap = BitmapFactory.decodeResource(resources, R.drawable.share_bg)
        val avatarBitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_round)
        val tempQr = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)

        val nameLabel = BitmapFactory.decodeResource(resources, R.drawable.my_ic_deposit)
        val builder = PosterCreator.Builder()
        builder.getPaint().textSize = DpUtils.dp2Px(this, 18).toFloat()
        val nameWidth = builder.getPaint().measureText("你好你好你好你好你好你好")

        val bitmap =
            builder
                .setDesignWidth(1500)
                .setDesignHeight(3248)
                .addImage(bgBitmap, Rect(0,0, bgBitmap.width, bgBitmap.height), Rect(0,0, 1500, 3248))

                .addImage(avatarBitmap, Rect(0,0, avatarBitmap.width, avatarBitmap.height), Rect(128,2832, 224, 2928))
                .addImage(tempQr, Rect(0,0, tempQr.width, tempQr.height), Rect(1084,2832, 1372, 3120))
                .addImage(nameLabel, Rect(0,0, nameLabel.width, nameLabel.height), Rect((nameWidth + 257).toInt(),2843,
                    (nameWidth + 435).toInt(), 2899))
                .addImage(
                    avatarBitmap, Rect(0, 0, avatarBitmap.width, avatarBitmap.height), Rect(192, 2896, 224, 2928)
                )
                .addTextBean(Color.parseColor("#242b29"), DpUtils.dp2Px(this, 18).toFloat(), Paint.Style.FILL, "你好你好你好你好你好你好", 248F, false, 2892F)
                .create()


        findViewById<ImageView>(R.id.test_img).setImageBitmap(bitmap)


        Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH)//获取本月最大天数

        Calendar.getInstance().get(Calendar.DAY_OF_WEEK)//获取周几 从1开始到7

        findViewById<View>(R.id.tv_test).setOnClickListener {
//            Log.e("info", "clicked button")
            val intent = Intent(this@TestActivity, MainActivity::class.java)
            intent.putExtra("image", BitmapFactory.decodeResource(resources, R.drawable.share_bg))
            startActivity(intent)
        }

        val recyclerView = findViewById<RecyclerView>(R.id.date_list)
        recyclerView.layoutManager = GridLayoutManager(this, 7)
        dates = CalendarUtil.getDays(2022, 5)
        /**
         * MONDAY值为2
         * SUNDAY值为1
         *
         * 从周一开始到周日时，前面可填充1号的week - MONDAY 个值，遇SUNDAY时换行；
         * 从周日开始到周六时，前面可填充1号的week - SUNDAY 个值，遇SATURDAY时换行；
         */
        for (i in 0 until (dates?.get(0)?.week?.minus(Calendar.MONDAY)!!)) {
            dates?.add(0, CalendarUtil.WeekDay(0, 0))
        }
        recyclerView.adapter = DateAdapter()


//        findViewById<ImageView>(R.id.image1).setImageBitmap(getSmallBitmap())
        textImage = findViewById<ImageView>(R.id.image1)
        textImage.setColorFilter(Color.RED)

        findViewById<SeekBar>(R.id.seekbar).setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val r = ((progress / 100f) * (0xff)).toInt()
                val g = ((progress / 100f) * (0xff)).toInt()
                val b = ((progress / 100f) * (0xff)).toInt()
                val color = ((0xFF shl 24) or (r shl 16) or (g shl 8) or b)
                textImage.setColorFilter(color)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        })
    }

    lateinit var textImage: ImageView

    inner class DateAdapter: RecyclerView.Adapter<DateHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateHolder {
            return DateHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_date, parent, false))
        }

        override fun onBindViewHolder(holder: DateHolder, position: Int) {
            holder.date.text = dates?.get(position)?.date.toString()
        }

        override fun getItemCount(): Int {
            return dates?.size ?:0
        }

    }

    inner class DateHolder(item:View):RecyclerView.ViewHolder(item){
        val date = item.findViewById<TextView>(R.id.item_date)
    }


    fun getSmallBitmap(): Bitmap{
        val options = BitmapFactory.Options()
//        options.inJustDecodeBounds = true
//        val bitmapFrame = BitmapFactory.decodeResource(resources, R.drawable.big_image, options)
//        var sampleSize = 1f
//        if (bitmapFrame.width > 1500) {
//            sampleSize = bitmapFrame.width / 1500F
//        }
//        options.inSampleSize = sampleSize.toInt()
        options.inSampleSize = 5
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeResource(resources, R.drawable.big_image, options)
    }
}